// Copyright (C) 2005-2008 Ilya S. Lyubinskiy. All rights reserved.
// Technical support: http://www.php-development.ru/
//
// YOU MAY NOT
// (1) Remove or modify this copyright notice.
// (2) Re-distribute this code or any part of it.
//     Instead, you may link to the homepage of this code:
//     http://www.php-development.ru/javascripts/popup-window.php
//
// YOU MAY
// (1) Use this code on your website.
// (2) Use this code as part of another product.
//
// NO WARRANTY
// This code is provided "as is" without warranty of any kind.
// You expressly acknowledge and agree that use of this code is at your own risk.

// USAGE
//
// function popup_show(id, drag_id, exit_id, position, x, y, position_id)
//
// id          - id of a popup window;
// drag_id     - id of an element within popup window intended for dragging it
// exit_id     - id of an element within popup window intended for hiding it
// position    - positioning type:
//               "element", "element-right", "element-bottom", "mouse",
//               "screen-top-left", "screen-center", "screen-bottom-right"
// x, y        - offset
// position_id - id of an element relative to which popup window will be positioned


// ***** Variables *************************************************************
var CRLF = String.fromCharCode(13) + String.fromCharCode(10);

function CloseDialog()
{
 var main = document.getElementById('main');
 e = document.getElementById('overlay');
 main.removeChild(e);
 var e = document.getElementById('diadiv');
 main.removeChild(e);

}

function BuildButton(ACaption,AClick)
{
 var buf = new StringBuffer();
 buf.xAppend(['<button ','onclick="javascript:',AClick,'">',ACaption,'</button>']);
 return buf.toString();
}

function BuildHRef(ARef)
{
 var buf = new StringBuffer();
 buf.xAppend(['href="javascript:',ARef,'"']);
 return buf.toString();
}

function BuildLink(AId,AClass,ACapt,ARef)
{
 var buf = new StringBuffer();
 buf.xAppend(['<a ',BuildHRef(ARef),' id="',AId,'">',ACapt,'</a>']);
 return buf.toString();
}

function BuildSpan(AId,AClass,AText)
{
 var buf = new StringBuffer();
 buf.xAppend(['<span class="',AClass,'" id="',AId,'">',AText,'</span>']);
 return buf.toString();
}

function BuildDiv(AId,AClass,AContents)
{
 var buf = new StringBuffer();
 buf.xAppend(['<div ','class="',AClass,'" id="',AId,'">',CRLF,AContents,CRLF,'</div>']);
 return buf.toString();
}

function BuildPara(AId,AClass,AContents)
{
 var buf = new StringBuffer();
 buf.xAppend(['<p ','class="',AClass,'" id="',AId,'">',CRLF,AContents,CRLF,'</p>']);
 return buf.toString();
}

function ShowDialog()
{
 var newdiv = document.createElement('div');
 var main = document.getElementById('main');
 newdiv.className = 'overlay';
 newdiv.id = 'overlay';
 var h = document.body.clientHeight + 'px';
 newdiv.style.height = h;
 var w = document.body.clientWidth - 40 + 'px';
 newdiv.style.width = w;
 main.appendChild(newdiv);
 
 newdiv = document.createElement('div');
 newdiv.className = 'diadiv';
 newdiv.id = 'diadiv';
 newdiv.style.height = h;
 newdiv.style.width = w;
 newdiv.innerHTML = MakeDialog();
 main.appendChild(newdiv); 
 AdjustSizes();
}

function AdjustSizes()
{
 var d = document.getElementById('dlg');
 if (d == null) return;
 var b = document.getElementById('dbody');
 var c = document.getElementById('capt');
 var t = document.getElementById('dtxt');
 var emPerpxX = 25/d.offsetWidth;
 var emPerpxY = 13/d.offsetHeight;
 
 b.style.height = (d.offsetHeight - c.offsetHeight)*emPerpxY + 'em';
 var w = d.offsetWidth*emPerpxX + 'em';
 b.style.width = w;
 c.style.width = w;
 t.style.height = b.offsetHeight*emPerpxY - 2 + 'em';
 t.style.width = (b.offsetWidth - 101)*emPerpxX + 'em';
}

function MakeDialog()
{
 var buf = new StringBuffer();
 buf.xAppend([BuildLink('close','diaqbut','x','CloseDialog()'),
              BuildLink('help','diaqbut','?',"alert('You need help?')"),
              BuildSpan('caption','capt','Sample Dialog')]);
 var s = buf.toString();
 buf.cleanUp();
 var capt = BuildDiv('capt','diacapt',s);
 
 s  = BuildPara('para','diapara','CSS Modal Dialog recipe from ExplainThat!') + 
      BuildDiv('btns','diaclose',BuildButton('Close','CloseDialog()'));
 s =    BuildDiv('dtxt','diatxt',s) + 
 BuildDiv('dimg','diaimg','<img width=101 height=94 src="/images/dlgimg.png"');
 s = BuildDiv('dbody','diabody',s);
 return BuildDiv('dlg','diaframe',capt + s);
}
/**********************************************************************
/
/
/    StringBuffer is a simple utility object which delivers
/     faster string concatenation than the Javascript + operator
/
***********************************************************************/
function StringBuffer() {this.buffer = []} 
StringBuffer.prototype.append = function append(string) {this.buffer.push(string)} 
StringBuffer.prototype.xAppend = 
function xAppend(strings)
{
 var i,ALen;
 ALen = strings.length;
 for (i=0;i<ALen;i++) this.buffer.push(strings[i]);
}
StringBuffer.prototype.cleanUp = function cleanUp(){this.buffer = []}
StringBuffer.prototype.toString = function toString(){return this.buffer.join("")}


var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;


// ***** popup_mousedown *******************************************************

function popup_mousedown(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  popup_mouseposX = ie ? window.event.clientX : e.clientX;
  popup_mouseposY = ie ? window.event.clientY : e.clientY;
}


// ***** popup_mousedown_window ************************************************

function popup_mousedown_window(e)
{
  var ie = navigator.appName == "Microsoft Internet Explorer";

  if ( ie && window.event.button != 1) return;
  if (!ie && e.button            != 0) return;

  popup_dragging = true;
  popup_target   = this['target'];
  popup_mouseX   = ie ? window.event.clientX : e.clientX;
  popup_mouseY   = ie ? window.event.clientY : e.clientY;

  if (ie)
       popup_oldfunction = document.onselectstart;
  else popup_oldfunction = document.onmousedown;

  if (ie)
       document.onselectstart = new Function("return false;");
  else document.onmousedown   = new Function("return false;");
}


// ***** popup_mousemove *******************************************************

function popup_mousemove(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;

  if (!popup_dragging) return;

  element.style.left = (element.offsetLeft+mouseX-popup_mouseX)+'px';
  element.style.top  = (element.offsetTop +mouseY-popup_mouseY)+'px';

  popup_mouseX = ie ? window.event.clientX : e.clientX;
  popup_mouseY = ie ? window.event.clientY : e.clientY;
}

// ***** popup_mouseup *********************************************************

function popup_mouseup(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  if (!popup_dragging) return;

  popup_dragging = false;

  if (ie)
       document.onselectstart = popup_oldfunction;
  else document.onmousedown   = popup_oldfunction;
}

// ***** popup_exit ************************************************************

function popup_exit(e)
{
  var ie      = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  popup_mouseup(e);
  element.style.display = 'none';
  var main = document.getElementById('main');
 e = document.getElementById('overlay');
 main.removeChild(e);

}


// ***** popup_show ************************************************************

function popup_show(id, drag_id, exit_id, position, x, y, position_id)
{

  var element      = document.getElementById(id);
  var drag_element = document.getElementById(drag_id);
  var exit_element = document.getElementById(exit_id);

  var width        = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
  var height       = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

  element.style.position = "absolute";
  element.style.display  = "block";
  
     
     

  	if (position == "element" || position == "element-right" || position == "element-bottom")
  	{
    	var position_element = document.getElementById(position_id);

    	for (var p = position_element; p; p = p.offsetParent)
      		if (p.style.position != 'absolute')
     		{
        		x += p.offsetLeft;
        		y += p.offsetTop;
      		}

    		if (position == "element-right" ) x += position_element.clientWidth;
    		if (position == "element-bottom") y += position_element.clientHeight;

	    		element.style.left = x+'px';
	    		element.style.top  = y+'px';
  			}

  	if (position == "mouse")
 	 {
    	element.style.left = (document.documentElement.scrollLeft+popup_mouseposX+x)+'px';
    	element.style.top  = (document.documentElement.scrollTop +popup_mouseposY+y)+'px';
  	}

 	 if (position == "screen-top-left")
  	{
   	 element.style.left = (document.documentElement.scrollLeft+x)+'px';
   	 element.style.top  = (document.documentElement.scrollTop +y)+'px';
  	}
	
  	if (position == "screen-center")
  	{
  	  element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )/2+x)+'px';
  	  element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)/2+y)+'px';
 	 }

 	 if (position == "screen-bottom-right")
 	 {
  	  element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )  +x)+'px';
  	  element.style.top  = (document.documentElement.scrollTop +(height-element.clientHeight)  +y)+'px';
 	 }

  	drag_element['target']   = id;
  	drag_element.onmousedown = popup_mousedown_window;
  	var newdiv = document.createElement('div');
	newdiv.className = 'overlay';
  newdiv.id = 'overlay';
	var h = document.body.clientHeight + 'px';
	 newdiv.style.height = h;
	 var w = document.body.clientWidth  + 'px';
	 newdiv.style.width = w;
    var main = document.getElementById('main');
     main.appendChild(newdiv);     
     element.className = 'diadiv'; 		
 main.appendChild(element); 
 //AdjustSizes();
  exit_element.onclick     = popup_exit;
}


// ***** Attach Events *********************************************************

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousedown', popup_mousedown);
else document.addEventListener('mousedown', popup_mousedown, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmousemove', popup_mousemove);
else document.addEventListener('mousemove', popup_mousemove, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent   ('onmouseup', popup_mouseup);
else document.addEventListener('mouseup', popup_mouseup, false);
